package com.star.goods.controller;

import com.github.pagehelper.Page;
import com.star.entity.PageResult;
import com.star.entity.Result;
import com.star.entity.StatusCode;
import com.star.goods.pojo.Brand;
import com.star.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/*
			  ________   ___   ___        ___   ________   ___   ___        ___
	 ________|\   __  \_|\  \_|\  \______|\  \_|\   __  \_|\  \_|\  \______|\  \_________
	 \       \ \  \_\ /_\ \  \\ \  \     \ \  \\ \  \_\ /_\ \  \\ \  \     \ \  \        \
	  \       \ \   __  \\ \  \\ \  \     \ \  \\ \   __  \\ \  \\ \  \     \ \  \        \
	   \       \ \  \_\  \\ \  \\ \  \____ \ \  \\ \  \_\  \\ \  \\ \  \____ \ \  \        \
	    \       \ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\        \
	     \       \|_______| \|__| \|_______| \|__| \|_______| \|__| \|_______| \|__|         \
	 	  \___________________________________________________________________________________\

*/
@RequestMapping("/brand")
@RestController
@CrossOrigin //解决跨域
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * query_all_brand
     * @return
     */
    @GetMapping
    public Result<List<Brand>> findAll() {
        List<Brand> brandList = brandService.findList();
        return new Result<>(true, StatusCode.OK,"查询成功",brandList);
    }

    /**
     * id查brand
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Brand brand = brandService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", brand);
    }

    /**
     * 新增品牌
     * @param brand
     * @return
     */
    @PostMapping()
    public Result add(@RequestBody Brand brand) {
        brandService.add(brand);
        return new Result(true,StatusCode.OK,"新增成功");
    }

    /**
     * 修改品牌
     * @param brand
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Brand brand,@PathVariable Integer id) {
        brand.setId(id);
        brandService.update(brand);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        brandService.delById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 品牌条件查询
     * @param searchMap
     * @return
     */
    @GetMapping("/search")
    public Result<List<Brand>> search(@RequestParam Map searchMap) {
        List<Brand> list = brandService.list(searchMap);
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable("page") int page,@PathVariable("size") int size) {
        Page<Brand> pageInfo = brandService.findPage(page, size);
        PageResult<Brand> pageResult = new PageResult<>(pageInfo.getTotal(), pageInfo.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    /**
     * 条件分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/searchPage/{page}/{size}")
    public Result findPage(@RequestParam Map searchMap, @PathVariable("page") int page, @PathVariable("size") int size) {
        Page<Brand> pageInfo = brandService.findPage(searchMap,page, size);
        PageResult<Brand> pageResult = new PageResult<>(pageInfo.getTotal(), pageInfo.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    /**
     * 根据分类名查品牌
     * @param category
     * @return
     */
    @GetMapping("/category/{category}")
    public Result findListByCategoryName(@PathVariable String category) {
        List<Map> brandList = brandService.findListByCategoryName(category);
        return new Result(true, StatusCode.OK, "查询成功", brandList);
    }
}
